<?PHP
require_once("connect.php");
class table_dao{
	private $dbconn,$TableName,$Table,$show_sql;
	private $sql=NULL;
	public $insertid=NULL;
	public function table_dao($tablename,$is_showSql = false){
		$this->TableName = $tablename;
		$this->dbconn = connect::getInstance();//new connect();
		$this->show_sql = $is_showSql;
		include(CONFIG_PATH."database.php");
		switch($tablename){
			case "feedback":
				$this->Table=$Table_feedback;
				break;
			case "keywords":
				$this->Table=$Table_keywords;
				break;
			case "history":
				$this->Table=$Table_history;
				break;
			case "price_cache":
				$this->Table=$Table_price_cache;
				break;
		}
	}
	public function get($searchdata = array(),$page=-1){//($key,$val){
		if($page==NULL){$page=-1;}
		if(is_array($searchdata)){
			$search_str = "where";
			foreach($searchdata as $search_key=>$search_val){
				if($search_key == "like"){
					if(empty($search_str)){$search_str.="where";}
					$search_str .= " $search_val ";
				}elseif($search_key=="orderby"){
          if("where"==$search_str){$search_str="";}
					$search_str .= " ORDER BY $search_val ";//ORDER BY uid ASC
				}elseif($this->ishavefield($search_key)){
					if(!empty($search_val)){
            if("where"!=$search_str){$search_str.=" and ";}
            $here_temp = NULL;
            $strs = explode(",",$search_val);
            foreach($strs as $strsval){
              if($here_temp!=NULL){$here_temp.="or";}
              $here_temp.=" `$search_key` = '".$this->_checkValue($strsval,true)."' ";
            }
            $search_str .= "(".$here_temp.")";
					}
					
				}
			}
			if($search_str=="where"){$search_str .=" 1";}
			$this->sql="select * from `".$this->TableName."` $search_str";
			if($this->show_sql){echo $this->sql;}
			$this->dbconn->query($this->sql,$page);
			return $this->dbconn->Result();
		}
		return NULL;
		//$this->dbconn->query("select * from `keywords` where `$key` = $val");
		//return $this->dbconn->Result();
	}
	public function GetCount(){
		return $this->dbconn->GetCount($this->sql);
	}
	public function add($data){
		$keystr = NULL;
		$valuestr =NULL;
		$Interval = NULL;
		$checkdata = $this->checkfield($data,true);
		if($checkdata!=NULL){
			foreach($checkdata as $key=>$val){
				$keystr .= $Interval."`".$key."`";
				$valuestr .= $Interval.$this->_checkValue($val)."";
				$Interval = ",";
			}
			if($this->show_sql){echo "INSERT INTO `".$this->TableName."` ($keystr)VALUES($valuestr)";}
			$this->dbconn->query("INSERT INTO `".$this->TableName."` ($keystr)VALUES($valuestr)");
		}
		// $Categoryid = mysql_insert_id();
		$this->insertid = mysql_insert_id();
	}
	public function updata($id,$data){
		if(is_array($data)){
			$sql_set =NULL;
			$checkdata = $this->checkfield($data,true);
			if(is_array($checkdata)){
        foreach($checkdata as $key=>$val){
          if($key!="Id"){
            $sql_set .=empty($sql_set)?"":",";
            $sql_set .= "`$key` =".$this->_checkValue($val);
          }
        }
			}
		}
		if(!empty($sql_set)){
      $this->dbconn->query("UPDATE `".$this->TableName."` SET $sql_set WHERE Id = {$id}");
		}
		if($this->show_sql){echo "UPDATE `".$this->TableName."` SET $sql_set WHERE Id = {$id}";}
	}
	public function del($id){
		$this->dbconn->query("DELETE FROM `".$this->TableName."` WHERE Id = {$id} ");
	}
	private function checkfield($data,$isinsert=false){
		$result=array();
		foreach($this->Table as $key=>$val){
			$data_key = checknull($key, $data);
			if(empty($data_key)){
				if($val=="NOTNULL"){return NULL;}
			}else{
        if($val!="id"){
          $result[$key] = "'".$data_key."'";
        }else if($val=="NULL"||$val=="null"){
          $result[$key] = "''";
        }
			}
			if($key=="createDate"&&$isinsert){
				$result[$key] = "NOW()";
			}
		}
		return $result;
	}
	private function ishavefield($checkkey){
		foreach($this->Table as $key=>$val){
			if($key==$checkkey){
				return true;
			}
		}
		return false;
	}
	private function _checkValue($input,$isselect = false) {
      $result = str_replace(
            array(
                '\\',
                '\'',
                '"',
            ),
            array(
                '\\\\',
                '\\\'',
                '&quot',
            ),
            $input
      );
      if($isselect  ){
          return $result;
      }else{
          return preg_replace("/\\\'(.*)\\\'/i","'$1'",$result);
      }
	}
}
?>